package goodroommate.ui.questionnaire;

import java.io.IOException;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import goodroommate.domain.Questionnaire;
import goodroommate.exception.DatabaseException;
import goodroommate.exception.ItemNotExistsException;
import goodroommate.service.IQuestionnaireService;
import goodroommate.service.impl.QuestionnaireService;

/**
 * Servlet implementation class ShowQuestionnaireServlet
 */
@WebServlet("/Questionnaire/GetAllQuestionnaire")
public class GetAllQuestionnaireServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	IQuestionnaireService questionnaireService = new QuestionnaireService();
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public GetAllQuestionnaireServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		try{
			ArrayList<Questionnaire>questionnaires=questionnaireService.getAllQuestionnaire();
			request.setAttribute("questionnaires", questionnaires);
		} catch (ItemNotExistsException e) {
			request.setAttribute("questionnaires", null);
		} catch (DatabaseException e) {
			response.sendError(500);
			return;
		}
		request.getRequestDispatcher("show_all_questionnaire.jsp").forward(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
